home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 …ember: Reference Library / Apple Developer Reference Library (December 1999) (Disk 1).iso / pc / technical documentation / develop / develop issue 20 / develop issue 20 code / scripting the finder.sea / Scripting the Finder / Zawphing / MacUtilities.cp / MacUtilities.cp
Encoding:
Text File  |  1994-10-11  |  5.4 KB  |  222 lines  |  [TEXT/MMCC]

  1. /*===================================================================
  2.     MacUtilities.c
  3.     
  4.     ©1991 Greg Anderson
  5.     greggor@apple.com
  6.  
  7.     Misc utility routines
  8. ===================================================================*/
  9. #include "MacUtilities.h"
  10.  
  11. #include <OSUtils.h>
  12.  
  13. Ptr gApplicStart = nil;
  14. Ptr gApplicEnd = nil;
  15. Ptr gSysStart = nil;
  16. Ptr gSysEnd = nil;
  17.  
  18. /*----------------------------------------------------------------------
  19. // PtrLooksValid
  20. //
  21. // Return true if the given pointer is even and non-nil
  22. ----------------------------------------------------------------------*/
  23. short PtrLooksValid( Ptr ptr )
  24. {
  25.     return ( (ptr != nil) && ((((long)ptr) & 1) == 0) );
  26. }
  27.  
  28. /*----------------------------------------------------------------------
  29. // PtrValid
  30. //
  31. // Return true if the given pointer looks valid and lies within
  32. // the range of our application zone or the system zone
  33. ----------------------------------------------------------------------*/
  34. short PtrValid( Ptr ptr )
  35. {
  36.     Boolean        valid = false;
  37.     
  38.     /*
  39.     // Don't bother with range checking if the pointer doesn't
  40.     // look valid
  41.     */
  42.     if( PtrLooksValid( ptr ) )
  43.     {
  44.         /*
  45.         // If our globals have not been set up, then
  46.         // initialize them
  47.         */
  48.         if( gApplicStart == nil )
  49.         {
  50.             THz        sysZone;
  51.             THz        applicZone;
  52.             
  53.             sysZone = SystemZone();
  54.             applicZone = ApplicZone();
  55.             
  56.             gApplicStart = (Ptr) applicZone;
  57.             gApplicEnd = (Ptr) (applicZone->bkLim);
  58.             
  59.             gSysStart = (Ptr) sysZone;
  60.             gSysEnd = (Ptr) (sysZone->bkLim);
  61.         }
  62.         
  63.         /*
  64.         // The pointer should lie within either our
  65.         // application or within the System zone.
  66.         // If it does not, we should not be playing
  67.         // with it.
  68.         */
  69.         if( ((ptr > gApplicStart) && (ptr < gApplicEnd)) || ((ptr > gSysStart) && (ptr < gSysEnd)))
  70.         {
  71.             valid = true;
  72.         } 
  73.     }
  74.     
  75.     return valid;
  76. }
  77.  
  78. /*----------------------------------------------------------------------
  79. // TheModifiers
  80. //
  81. // Return the state of the modifier keys
  82. ----------------------------------------------------------------------*/
  83. short TheModifiers( )
  84. {
  85.     EventRecord        event;
  86.     
  87.     EventAvail( 0, &event );
  88.     return( event.modifiers );
  89. }
  90.  
  91. /*----------------------------------------------------------------------
  92. // OptionKeyIsDown
  93. //
  94. // Return true if the option key is down
  95. ----------------------------------------------------------------------*/
  96. Boolean OptionKeyIsDown( )
  97. {
  98.     return( (TheModifiers() & optionKey) != 0 );
  99. }
  100.  
  101. /*----------------------------------------------------------------------
  102. // CommandKeyIsDown
  103. //
  104. // Return true if the command key is down
  105. ----------------------------------------------------------------------*/
  106. Boolean CommandKeyIsDown( )
  107. {
  108.     return( (TheModifiers() & cmdKey) != 0 );
  109. }
  110.  
  111. /*----------------------------------------------------------------------
  112. // ShiftKeyIsDown
  113. //
  114. // Return true if the shift key is down
  115. ----------------------------------------------------------------------*/
  116. Boolean ShiftKeyIsDown( )
  117. {
  118.     return( (TheModifiers() & shiftKey) != 0 );
  119. }
  120.  
  121. /*----------------------------------------------------------------------
  122. // RgnToGlobal
  123. //
  124. // Convert a region from local coordinates to global coordinates
  125. ----------------------------------------------------------------------*/
  126. void RgnToGlobal( RgnHandle aRegion )
  127. {
  128.     Point        refPoint;
  129.     
  130.     refPoint.h = 0;
  131.     refPoint.v = 0;
  132.     LocalToGlobal(&refPoint);
  133.     OffsetRgn( aRegion, refPoint.h, refPoint.v );
  134. }
  135.  
  136. /*----------------------------------------------------------------------
  137. // RgnToLocal
  138. //
  139. // Convert a region from global coordinates to local coordinates
  140. ----------------------------------------------------------------------*/
  141. void RgnToLocal( RgnHandle aRegion )
  142. {
  143.     Point        refPoint;
  144.     
  145.     refPoint.h = 0;
  146.     refPoint.v = 0;
  147.     LocalToGlobal(&refPoint);
  148.     OffsetRgn( aRegion, -refPoint.h, -refPoint.v );
  149. }
  150.  
  151. /*----------------------------------------------------------------------
  152. // RectToGlobal
  153. //
  154. // Convert a rectangle from local coordinates to global coordinates
  155. ----------------------------------------------------------------------*/
  156. void RectToGlobal(Rect* aRect)
  157. {
  158.     Point        refPoint;
  159.     
  160.     refPoint.h = 0;
  161.     refPoint.v = 0;
  162.     LocalToGlobal(&refPoint);
  163.     OffsetRect(aRect, refPoint.h, refPoint.v );
  164. }
  165.  
  166. /*----------------------------------------------------------------------
  167. // RectToLocal
  168. //
  169. // Convert a rectangle from global coordinates to local coordinates
  170. ----------------------------------------------------------------------*/
  171. void RectToLocal(Rect* aRect)
  172. {
  173.     Point        refPoint;
  174.     
  175.     refPoint.h = 0;
  176.     refPoint.v = 0;
  177.     LocalToGlobal(&refPoint);
  178.     OffsetRect(aRect, -refPoint.h, -refPoint.v );
  179. }
  180.  
  181. /*----------------------------------------------------------------------
  182. // GetGlobalWindowLocation
  183. //
  184. // Return the location of a window in global coordinates
  185. ----------------------------------------------------------------------*/
  186. void GetGlobalWindowLocation(WindowPtr window, Rect* windowRect)
  187. {
  188.     *windowRect = ((WindowPeek)window)->port.portRect;
  189.     RectToGlobal(windowRect);
  190. }
  191.  
  192. /*-----------------------------------------------------------------
  193. // ChangeCursor
  194. //
  195. // Change the shape of the cursor
  196. -----------------------------------------------------------------*/
  197. void ChangeCursor( short cursID )
  198. {
  199.     CursHandle    curs;
  200.     short        state;
  201.     
  202.     /*
  203.     // Special checking:  ID zero == arrow
  204.     */
  205.     if( !cursID )
  206.     {
  207.         SetCursor( &qd.arrow );
  208.         return;
  209.     }
  210.     /*
  211.     // Get the cursor resource & set its shape
  212.     */
  213.     curs = GetCursor(cursID);
  214.     if( curs )
  215.     {
  216.         state = HGetState((Handle)curs);
  217.         HLock((Handle)curs);
  218.         SetCursor(*curs);
  219.         HSetState((Handle)curs,state);
  220.     }
  221. }
  222.